"""
对话消息表
"""
from sqlalchemy import Column, Integer, String, Text, Boolean, DateTime, ForeignKey, JSON
from sqlalchemy.sql import func
from sqlalchemy.orm import relationship
from app.models.base import Base


class ChatMessage(Base):
    __tablename__ = "chat_messages"

    id = Column(Integer, primary_key=True, index=True)
    conversation_id = Column(Integer, ForeignKey("chat_conversations.id"), nullable=False, comment="对话ID")
    role = Column(String(20), nullable=False, comment="角色: user/assistant/system")
    content = Column(Text, nullable=False, comment="消息内容")
    tokens = Column(Integer, nullable=True, comment="token数量")
    message_metadata = Column(JSON, default={}, comment="额外元数据")
    is_deleted = Column(Boolean, default=False, comment="是否删除")
    created_at = Column(DateTime(timezone=True), server_default=func.now())

    # 关系
    conversation = relationship("ChatConversation", back_populates="messages")